//
//  LYTabBarController.m
//  lottery
//
//  Created by 赵国腾 on 16/6/10.
//  Copyright © 2016年 赵国腾. All rights reserved.
//

#import "LYTabBarController.h"
#import "LYHallViewController.h"
#import "LYArenaViewController.h"
#import "LYDiscoverViewController.h"
#import "LYHistoryViewController.h"
#import "LYMyLotteryViewController.h"
#import "LYTabBar.h"
#import "LYNavigationViewController.h"
#import "LYArenaNaviViewController.h"

@interface LYTabBarController ()<LYTabBarDelegate>

/** items */
@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation LYTabBarController

- (NSMutableArray *)items {
    
    if (_items == nil) {
        _items = [NSMutableArray array];
    }
    
    return _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加自控制器
    [self setupAllChildViewController];
    
    // 自定义TabBar
    [self setupTabBar];

}

- (void)setupAllChildViewController {
    
    // 购彩大厅
    LYHallViewController *hall = [[LYHallViewController alloc] init];
    [self setupOneChildViewController:hall
                                image:[UIImage imageNamed:@"TabBar_LotteryHall_new"]
                          selectImage:[UIImage imageNamed:@"TabBar_LotteryHall_selected_new"]
                                title:@"购彩大厅"];
        
    // 竞技场
    LYArenaViewController *arena = [[LYArenaViewController alloc] init];
    [self setupOneChildViewController:arena
                                image:[UIImage imageNamed:@"TabBar_Arena_new"]
                          selectImage:[UIImage imageNamed:@"TabBar_Arena_selected_new"]
                                title:@"竞技场"];
    
    // 发现
    LYDiscoverViewController *discover = [LYDiscoverViewController discoverViewController];
    [self setupOneChildViewController:discover
                                image:[UIImage imageNamed:@"TabBar_Discovery_new"]
                          selectImage:[UIImage imageNamed:@"TabBar_Discovery_selected_new"]
                                title:@"发现"];
    
    // 开奖信息
    LYHistoryViewController *history = [[LYHistoryViewController alloc] init];
    [self setupOneChildViewController:history
                                image:[UIImage imageNamed:@"TabBar_History_new"]
                          selectImage:[UIImage imageNamed:@"TabBar_History_selected_new"]
                                title:@"开奖信息"];

    // 我的彩票
    LYMyLotteryViewController *myLottery = [[LYMyLotteryViewController alloc] init];
    [self setupOneChildViewController:myLottery
                                image:[UIImage imageNamed:@"TabBar_MyLottery_new"]
                          selectImage:[UIImage imageNamed:@"TabBar_MyLottery_selected_new"]
                                title:@"我的彩票"];
    
}

#pragma mark - 自定义TabBar
- (void)setupTabBar {
    
    // 移除系统的TabBar, 相当于把TabBar上面的按钮移除
//    [self.tabBar removeFromSuperview];

    // 视图移除的时候不会立即销毁，会在下一个运行循环开始的时候销毁
    LYTabBar *tabBar = [[LYTabBar alloc] init];
    tabBar.delegate = self;
    tabBar.backgroundColor = [UIColor orangeColor];
    tabBar.items = self.items;
    tabBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:tabBar];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    for (UIView *view in self.tabBar.subviews) {
        
        if (![view isKindOfClass:[LYTabBar class]]) {
            
            [view removeFromSuperview];
        }
        
    }
}

#pragma mark - <LYTabBarDelegate>
- (void)tabBar:(LYTabBar *)tabBar didClickIndex:(NSInteger)index {
    
    self.selectedIndex = index;
}

#pragma mark - 添加一个自控制器
- (void)setupOneChildViewController:(UIViewController *)vc
                              image:(UIImage *)image
                        selectImage:(UIImage *)selectImage
                              title:(NSString *)title{
    
    
//    vc.view.backgroundColor = [self randomColor];
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectImage;
    vc.navigationItem.title = title;
    [self.items addObject:vc.tabBarItem];
    
    UINavigationController *naviCtrl = [[LYNavigationViewController alloc] initWithRootViewController:vc];
    
    if ([vc isKindOfClass:[LYArenaViewController class]]) {
        
        // 下面方法会调用- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
        naviCtrl = [[LYArenaNaviViewController alloc] initWithRootViewController:vc];
    }
    
    [self addChildViewController:naviCtrl];
}

- (UIColor *)randomColor {
    
    CGFloat r = arc4random_uniform(256) / 255.0;
    CGFloat g = arc4random_uniform(256) / 255.0;
    CGFloat b = arc4random_uniform(256) / 255.0;

    return [UIColor colorWithRed:r green:g blue:b alpha:1.0];
}

@end








